package com.mazaiting.instructions.comparisons;

import com.mazaiting.instructions.base.BranchInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtObject;
import com.mazaiting.rtda.MtOperandStack;

/**
 * if_acmpeq和if_acmpne指令把栈顶的两个引用弹出，根据引用是否相同进行跳转
 *
 * @author mazaiting
 * @since 2021/8/15
 */
public class IfACmpInstruction {

    /**
     * 比较引用是否相等
     */
    public static class IfACmpEq extends BranchInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            MtObject ref1 = stack.popRef();
            MtObject ref2 = stack.popRef();
            if (ref1 == ref2) {
                branch(frame, offset);
            }
        }
    }

    /**
     * 比较引用是否不相等
     */
    public static class IfACmpNe extends BranchInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            MtObject ref1 = stack.popRef();
            MtObject ref2 = stack.popRef();
            if (ref1 != ref2) {
                branch(frame, offset);
            }
        }
    }
}
